package com.hz.api.model.request;

import com.alibaba.fastjson2.JSON;
import com.hz.api.model.enums.ApiResponseCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ApiResponse<T> {

    public String code;

    public String message;

    public T data;

    public ApiResponse(String code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 通用的成功方法，只需传入数据即可
     * @param data 需要返回的数据
     * @return JSON
     * @param <T> 数据类型
     */
    public static <T> String ok(T data) {
        ApiResponse<T> res = new ApiResponse<>(ApiResponseCodeEnum.SUCCESS.getCode(), ApiResponseCodeEnum.SUCCESS.getMessage(), data);
        return JSON.toJSONString(res);
    }

    /**
     * 通用的资源无权访问方法
     * @return JSON
     */
    public static String resForbidden() {
        ApiResponse res = new ApiResponse<>(ApiResponseCodeEnum.RESOURCE_FORBIDDEN.getCode(), ApiResponseCodeEnum.RESOURCE_FORBIDDEN.getMessage());
        return JSON.toJSONString(res);
    }

    /**
     * 通用的接口禁止方法
     * @return JSON
     */
    public static String intForbidden() {
        ApiResponse res = new ApiResponse<>(ApiResponseCodeEnum.INTERFACE_FORBIDDEN.getCode(), ApiResponseCodeEnum.INTERFACE_FORBIDDEN.getMessage());
        return JSON.toJSONString(res);
    }

    /**
     * 通用的资源不存在方法
     * @return JSON
     */
    public static String resNotFound() {
        ApiResponse res = new ApiResponse<>(ApiResponseCodeEnum.RESOURCE_NOT_FOUND.getCode(), ApiResponseCodeEnum.RESOURCE_NOT_FOUND.getMessage());
        return JSON.toJSONString(res);
    }

    /**
     * 通用的接口错误方法
     * @return JSON
     */
    public static String intError() {
        ApiResponse res = new ApiResponse<>(ApiResponseCodeEnum.INTERFACE_ERROR.getCode(), ApiResponseCodeEnum.INTERFACE_ERROR.getMessage());
        return JSON.toJSONString(res);
    }

    /**
     * 通用的系统错误方法
     * @return JSON
     */
    public static String sysError() {
        ApiResponse res = new ApiResponse<>(ApiResponseCodeEnum.SYSTEM_ERROR.getCode(), ApiResponseCodeEnum.SYSTEM_ERROR.getMessage());
        return JSON.toJSONString(res);
    }


}
